package com.face.server.common.domain;


import com.face.server.common.utils.DataPacketUtil;

import java.nio.ByteBuffer;

/**
 * @author yangzhongming
 * @desc 【人脸识别出纸机】--协议数据格式对象
 * @date 2022/1/24
 */
public class ByteArrayReceiverEx {
    /**
     * 协议编号
     */
    private byte order;
    /**
     * 数据长度
     */
    private Integer length;
    /**
     * 数据字节数组
     */
    private byte[] content;

    public byte getOrder() {
        return order;
    }

    public void setOrder(byte order) {
        this.order = order;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(DataPacketUtil.byteToHexStr(order));
        builder.append(" ");
        builder.append(DataPacketUtil.byteToHexStr(DataPacketUtil.intToBytes(length, 2)));
        if (content != null) {
            builder.append(DataPacketUtil.byteToHexStr(content));
        }
        return builder.toString();
    }

    public byte[] toArray() {
        ByteBuffer buffer = ByteBuffer.allocate(length + 3);
        buffer.put(order);
        buffer.put(DataPacketUtil.intToBytes(length, 2));
        buffer.put(content);
        return buffer.array();
    }
}
